<?php

/* Load widget with widget_init function */
add_action( 'widgets_init', 'recent_tweets' );

/* Register widget */
function recent_tweets() {
	register_widget( 'recent_tweets' );
}

/* Handler class for all widget params */
class recent_tweets extends WP_Widget {

	/* Widget setup. */
	function recent_tweets() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'twitter', 'description' => __('Your recent tweets.', 'twitter') );
		/* Widget control settings. */
		$control_ops = array( 'width' => 250, 'height' => 400, 'id_base' => 'twitter' );
		/* Create the widget. */
		$this->WP_Widget( 'twitter', 'Recent Tweets', $widget_ops, $control_ops );
	}

	/** Widget display template */
	function widget( $args, $instance ) {
		extract( $args );

		/* Get widget settings */
		$title = apply_filters('widget_title', $instance['title'] );
		$id = $instance['id'];
		$max_tweets = $instance['number'];
		
		/* Widget themplate */
		echo $before_widget;
		
	   	if ( $title ) echo $before_title . $title . $after_title;

		$user = str_replace('&nbsp;', '', $id);  
       	if( !class_exists( 'WP_Http' ) ) include_once( ABSPATH . WPINC. '/class-http.php' );

		$link = "http://twitter.com/statuses/user_timeline/".$user.".json";
		$the_http = new WP_Http;
		$result = $the_http->request($link);
		
		if ( $result ) 
		{
			$the_tweets = json_decode($result['body'], true);
			echo '<ul>';
				$tweet_count = 0;
				foreach( $the_tweets as $tweet )
				{
					$tweet_count++;
					$pattern = '/\@(\w+)/';
					$replace = '<a target="_blank" rel="nofollow" href="http://twitter.com/$1">$0</a>';
					
					$tweet['text'] = preg_replace($pattern, $replace , $tweet['text']);
					
					echo '<li>';
						echo '<div class="content">';
							echo make_clickable($tweet['text']);
						echo '</div>';
						echo '<div class="date">';
							echo date("d F Y - g:i A",strtotime($tweet['created_at']));
						echo '</div>';						
					echo '</li>';	
					if ( $tweet_count == $max_tweets ) break;					
				} // END foreach
			echo '</ul>';
			echo '<a class="follow_on_twitter" href="http://www.twitter.com/'.$user.'">Follow on twitter</a>';
		} // END if
		else echo '<div class="twitter_error">Error requiring the lastest tweets. Try again later.</div>';

		echo $after_widget;
	}

	/* Update widget settings. */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Create widget settings instances. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['id'] = $new_instance['id'];
		$instance['number'] = $new_instance['number'];
		return $instance;
	}

	/* Admin panel form  */
	function form( $instance ) {
		$instance = wp_parse_args( (array) $instance ); 

		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('title'); echo '">'; echo 'Widget Title'; echo '</label>';
			echo '<br />'; 
			echo '<input type="text" class="widefat" id="'; echo $this->get_field_id('title'); echo '" name="'; echo $this->get_field_name('title'); echo '" value="'; if ( isset($instance['title']) ) echo $instance['title']; echo '" />';
		echo '</p>';
       
		echo '<p>'; 
			echo '<label for="'; echo $this->get_field_id('id'); echo '">'; echo 'Twitter User:'; echo '</label>';
			echo '<br />';
			echo '<input type="text" class="widefat" id="'; echo $this->get_field_id('id'); echo '" name="'; echo $this->get_field_name('id'); echo '" value="'; if ( isset($instance['id']) ) echo $instance['id']; echo '" />';
		echo '</p>';
        
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('number'); echo '">'; echo 'Number of Tweets:'; echo '</label>';
            echo '<br />';
			echo '<input type="text" class="widefat" id="'; echo $this->get_field_id('number'); echo '" name="'; echo $this->get_field_name('number'); echo '" value="'; if ( isset($instance['number']) ) echo $instance['number']; echo '" />';
		echo '</p>';
        

	}
}

?>